package servlet;

import pojo.Category;
import utils.MybatsUtils;
import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;

import java.io.IOException;
import java.util.List;

//搜索按钮 逻辑实现

@WebServlet(name = "FindMusicServlet", value = "/FindMusic")
public class FindMusicServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");
        //1 获取参数

        String category_id =request.getParameter("category_id"); //音乐的分类
//        获取查询的结果
        List<Category> userList = MybatsUtils.selectList("CategoryMapper.findCategoryByName",category_id);

        //2 将查询到的音乐信息保存到session中
        HttpSession session = request.getSession();
        session.setAttribute("allMusics", userList);

        //3 页面跳转到 musics.jsp
        request.getRequestDispatcher("Music.jsp").forward(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }
}
